package com.abel.bigwater.dataserver.model.kming

import io.netty.buffer.ByteBuf
import java.util.*

class LimitStatus : TermData(KmingDataType.TermDummy, 2) {
    var back = BitSet(16)

    var upperLimitWarn20: Boolean = false

    var lowerLimitWarn21: Boolean = false

    var zeroWarn24: Boolean = false


    override fun bufLength(): Int {
        return 2
    }

    override fun parseBuf(buf: ByteBuf): Boolean {
        back = BitSet.valueOf(ByteArray(2).apply { buf.readBytes(this) }).apply {
            upperLimitWarn20 = get(8)
            lowerLimitWarn21 = get(9)

            zeroWarn24 = get(12)
        }

        return true
    }

    override fun pack(buf: ByteBuf) {
        back.apply {
            set(8, upperLimitWarn20)
            set(9, lowerLimitWarn21)
            set(12, zeroWarn24)
        }

        val zeroArray = byteArrayOf(0, 0)
        buf.writeBytes(if (back.size() == 0) zeroArray else back.toByteArray().plus(zeroArray).copyOf(2))
    }
}